package com.xingxuecn.limit.dao.impl;

import com.xingxuecn.limit.common.model.Header;
import com.xingxuecn.limit.common.utils.DbUtils;
import com.xingxuecn.limit.dao.HeaderDao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zgx at 2018/9/10 0010 下午 5:35
 */
public class HeaderDaoImpl implements HeaderDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public List<Header> findAll() throws SQLException {
        String sql = "select * from header";
        Connection connection = DbUtils.getConnection();
        List<Header> result;
        try {
            result = qr.query(connection, sql, new BeanListHandler<Header>(Header.class));
        } finally {
            connection.close();
        }
        return result;
    }

    @Override
    public List<Header> search(Header header, Integer... limit) throws SQLException {
        StringBuffer sql = new StringBuffer("select * from header where 1=1");
        List<Object> jdbcParams = new LinkedList<>();
        Connection connection = DbUtils.getConnection();
        List<Header> result;
        try {
            result = qr.query(connection, sql.toString(), new BeanListHandler<Header>(Header.class), jdbcParams.toArray());
        } finally {
            connection.close();
        }
        return result;
    }
}
